package cn.dt.ymcc.web.controller;

import cn.dt.ymcc.GlobalException;
import cn.dt.ymcc.domain.WechatDto;
import cn.dt.ymcc.service.ILoginService;
import cn.dt.ymcc.domain.Login;
import cn.dt.ymcc.query.LoginQuery;
import cn.dt.ymcc.result.JSONResult;
import cn.dt.ymcc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    public ILoginService loginService;

    /**
     * 保存和修改公用的
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody Login login) {

        loginService.insert(login);

        return JSONResult.success(login.getId());
    }

    /**
     * 删除对象
     */

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id) {
        loginService.deleteById(id);
        return JSONResult.success();
    }

    /**
     * 获取对象
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id") Long id) {
        return JSONResult.success(loginService.selectById(id));
    }
    /**
     * 获取对象
     */
    @RequestMapping("/loginSuccess")
    @ResponseBody
    public String loginSuccess() {
        return "登录成功";
    }


    /**
     * 查询所有对象
     */
    @PreAuthorize("hasAuthority('login:list')")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public JSONResult list() {
        return JSONResult.success(loginService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @RequestMapping(value = "/pagelist", method = RequestMethod.POST)
    public JSONResult page(@RequestBody LoginQuery query) {
        Page<Login> page = new Page<Login>(query.getPage(), query.getRows());
        page = loginService.selectPage(page);
        return JSONResult.success(new PageList<Login>(page.getTotal(), page.getRecords()));
    }

    /**
     * 请求认证授权 中间接口
     * @param login
     * @return
     */
    @RequestMapping(value="/common",method= RequestMethod.POST)
    public JSONResult common(@RequestBody Login login){
        return loginService.common(login);
    }
    /**
     * 刷新token
     * @param login
     * @return
     */
    @RequestMapping(value="/refresh",method= RequestMethod.POST)
    public JSONResult refresh(@RequestBody Login login){
        return loginService.refresh(login);
    }

    /**
     * 三方微信登录
     *
     * @param dto
     * @return
     */
    @RequestMapping("/wechat")
    public JSONResult wechat(@RequestBody WechatDto dto) {
        return loginService.wechat(dto);
    }
}
